
package com.wnhoo.smartfishtank.api;

import com.google.gson.Gson;
import com.wnhoo.smartfishtank.api.data.LM_DataReceive;
import com.wnhoo.smartfishtank.api.data.LM_DataSend;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import java.io.IOException;

/**
 * Created by ThinkPadE431 on 2015/4/11.
 */
public class LM_USBAPI {


    public static final String SOURCE = ",1";
    public static final int USB_TYPE_24G_SOCKET = 12;
    public static final int USB_TYPE_24G_LED = 13;
    public static final int USB_TYPE_24G_RGB = 16;
    public static final int USB_TYPE_HUMITRUE = 15;


    private static final Gson gson;

    static {

        gson = new Gson();
    }
    public static String getNameDefault(String id,int type){
        String name = "";
        switch (type){
            case USB_TYPE_24G_SOCKET:
                name = "2.4G插座";
                break;
            case USB_TYPE_24G_LED:
                name = "LED灯";
                break;
            case USB_TYPE_24G_RGB:
                name = " RGB灯";
                break;
            case USB_TYPE_HUMITRUE:
                name = "温湿度";
                break;
            default:
                break;
        }
        return name + id;
    }

    public static LM_DataSend requestSendAddUSBDevice(boolean isDebug,String deviceId,String USB_ID,String uuid_tag,
                                                 String token_value) throws ClientProtocolException, IOException,JSONException {
        String sensorId = "S11";
        String sensorType = "1";
        String data = USB_ID+",SET,"+uuid_tag;
        //MyLog.TT("USBAPI", "requestSendAddUSBDevice data->" + data);

        String result = LM_BasicAPI.requestSend(isDebug, deviceId, sensorId, sensorType, data, token_value);

        return gson.fromJson(result,LM_DataSend.class);
    }
    public static LM_DataReceive requestReceiveAddUSBDevice(boolean isDebug,String deviceId,
                                                       String token_value) throws ClientProtocolException, IOException ,JSONException{
        String sensorId = "S11";
        String sensorType = "1";
        String result = LM_BasicAPI.requestReceive(isDebug,deviceId,sensorId,sensorType,token_value);
        return gson.fromJson(result,LM_DataReceive.class);
    }
    public static LM_DataSend requestSendRemoveUSBDevice(boolean isDebug,String deviceId,String USB_ID,String uuid_tag,
                                                      String token_value) throws ClientProtocolException, IOException,JSONException {
        String sensorId = "S11";
        String sensorType = "1";
        String data = USB_ID+",CLR,"+uuid_tag;
        String result = LM_BasicAPI.requestSend(isDebug,deviceId,sensorId,sensorType,data,token_value);

        return gson.fromJson(result,LM_DataSend.class);
    }

    public static LM_DataReceive requestReceiveRemoveUSBDevice(boolean isDebug,String deviceId,
                                                            String token_value) throws ClientProtocolException, IOException ,JSONException{
        return requestReceiveAddUSBDevice(true,deviceId,token_value);
    }
    /**
     * Send S10/1/-1
     *
     * @param isDebug
     * @param deviceId
     * @param token_value
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * @throws JSONException
     */
    public static LM_DataSend requestSendUSBType(boolean isDebug,String deviceId,
                                             String token_value) throws ClientProtocolException, IOException,JSONException {
        String sensorId = "S10";
        String sensorType = "1";
        String data = "-1";
        String result = LM_BasicAPI.requestSend(isDebug,deviceId,sensorId,sensorType,data,token_value);

        return gson.fromJson(result,LM_DataSend.class);
    }

    /**
     * receive result for S10/1/-1
     *
     * @param isDebug
     * @param deviceId
     * @param token_value
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * @throws JSONException
     */
    public static LM_DataReceive requestReceiveUSBType(boolean isDebug,String deviceId,
                                                        String token_value) throws ClientProtocolException, IOException ,JSONException{
        String sensorId = "S10";
        String sensorType = "1";
        String result = LM_BasicAPI.requestReceive(isDebug,deviceId,sensorId,sensorType,token_value);
        return gson.fromJson(result,LM_DataReceive.class);
    }

    public static LM_DataSend requestSendRGB(boolean isDebug,String deviceId,
                                             String data,String token_value) throws ClientProtocolException, IOException,JSONException {
        String sensorId = "S16";
        String sensorType = "1";
        String result = LM_BasicAPI.requestSend(isDebug,deviceId,sensorId,sensorType,data,token_value);

        return gson.fromJson(result,LM_DataSend.class);
    }
    public static LM_DataSend requestSendLED(boolean isDebug,String deviceId,
                                             String data,String token_value) throws ClientProtocolException, IOException ,JSONException{
        String sensorId = "S13";
        String sensorType = "1";
        String result = LM_BasicAPI.requestSend(isDebug,deviceId,sensorId,sensorType,data,token_value);

        return gson.fromJson(result,LM_DataSend.class);
    }
    public static LM_DataReceive requestReceiveLED(boolean isDebug,String deviceId,
                                                         String token_value) throws ClientProtocolException, IOException ,JSONException{
        String sensorId = "S13";
        String sensorType = "1";
        String result = LM_BasicAPI.requestReceive(isDebug,deviceId,sensorId,sensorType,token_value);
        return gson.fromJson(result,LM_DataReceive.class);
    }

    public static LM_DataSend requestSendUSBSocket(boolean isDebug,String deviceId,
                                             String usb_id,boolean switch_status,String token_value) throws ClientProtocolException, IOException ,JSONException{
        String sensorId = "S12";
        String sensorType = "1";
        StringBuffer data = new StringBuffer(usb_id);
        data.append(",").append(switch_status?1:0).append(SOURCE);
        String result = LM_BasicAPI.requestSend(isDebug,deviceId,sensorId,sensorType,data.toString(),token_value);

        return gson.fromJson(result,LM_DataSend.class);
    }
    public static LM_DataReceive requestReceiveUSBSocket(boolean isDebug,String deviceId,
                                                   String token_value) throws ClientProtocolException, IOException ,JSONException{
        String sensorId = "S12";
        String sensorType = "1";
        String result = LM_BasicAPI.requestReceive(isDebug,deviceId,sensorId,sensorType,token_value);
        return gson.fromJson(result,LM_DataReceive.class);
    }
    public static LM_DataReceive requestReceiveHumitrue(boolean isDebug,String deviceId,
                                                   String token_value) throws ClientProtocolException, IOException ,JSONException{
        String sensorId = "S15";
        String sensorType = "1";
        String result = LM_BasicAPI.requestReceive(isDebug,deviceId,sensorId,sensorType,token_value);
        return gson.fromJson(result,LM_DataReceive.class);
    }
}
